package Sort2017;

/**
 * 希尔排序
 * Created by 雷文康 on 2017/12/25.
 */
public class ShellSort {

    public static void main(String[] args) {
        int[] array={9,7,8,6,5,4,3,2,1};
        shellSort(array);
        for(int i=0;i<array.length;i++){
            System.out.print(array[i]);
        }
    }
    public static void shellSort(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }
        int increment = array.length;
        do {
            increment = increment / 3 + 1;
            for (int i = increment; i < array.length; i++) {
                if (array[i] < array[i - increment]) {
                    int val = array[i];
                    int j;
                    for (j = i - increment; j >= 0 && array[j] > val; j = j - increment) {
                        array[j + increment] = array[j];
                    }
                    array[j + increment] = val;
                }
            }
        } while (increment > 1);
    }
}
